package com.jsnkj.basic.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.jsnkj.basic.R
import com.jsnkj.basic.util.MediaUtil
import com.jsnkj.basic.util.QRCodeUtil


/**
 * 分享 邀请弹窗
 */
class GoInviteDialog(
    var dialogContext: Context,
    var content: String,
    var click: onItemClickListener
) : Dialog(dialogContext), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_go_invite)
        setCanceledOnTouchOutside(true)
        setCancelable(true)
        window?.setBackgroundDrawableResource(android.R.color.transparent)
        initView()
    }

    var bitmapQR: Bitmap? = null
    fun initView() {
        bitmapQR = QRCodeUtil.createQRCodeBitmap(
            content, 200, 200,
            "UTF-8", "H", "1", Color.BLACK, Color.WHITE
        )

        if (null != bitmapQR) {
            val imgQr: ImageView = findViewById(R.id.img_qr)
            Glide.with(dialogContext).load(bitmapQR).into(imgQr)
        }


        val tvShare: TextView = findViewById(R.id.tv_share)
        tvShare.setOnClickListener(this)

        val tvSave: TextView = findViewById(R.id.tv_save)
        tvSave.setOnClickListener(this)


    }


    interface onItemClickListener {
        fun shareItemClick()

        fun saveItemClick(bitmap: Bitmap?)
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.tv_share -> {
                dismiss()
                click.shareItemClick()
            }

            R.id.tv_save -> {
                dismiss()
                val conLayoutBg: ConstraintLayout = findViewById(R.id.con_layout_bg)
                click.saveItemClick(MediaUtil.getViewBitmap(conLayoutBg))
            }
        }
    }
}